﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;
using ApplicationLibrary;
using System.Text.RegularExpressions;


namespace Clothes.Items.Libraries
{
    class ItemTextDescription : TextBox
    {
        private const string tagCategory = "categoria";
        private const string tagDesigner = "designer";
        private const string tagSize = "taglia";
        private const string tagColor = "colore";
        private const string tagPrint = "stampa";
        private const string tagMaterial = "materiale";

        private string designer, category, material, print, size, color;


        #region Public Properties
        public string OriginalValue { get; set; }

        [DefaultValue("")]
        public string StringFormat { get; set; }
         [DefaultValue("")]
        public string Designer
        {
            get { return designer; }
            set { designer = value; ChangeDescription(); }
        }
         [DefaultValue("")]
        public string Category
        {
            get { return category; }
            set { category = value; ChangeDescription(); }
        }
         [DefaultValue("")]
        public string Material
        {
            get { return material; }
            set { material = value; ChangeDescription(); }
        }
         [DefaultValue("")]
        public string Print
        {
            get { return print; }
            set { print = value; ChangeDescription(); }
        }
         [DefaultValue("")]
        public string Sizes
        {
            get { return size; }
            set { size = value; ChangeDescription(); }
        }
         [DefaultValue("")]
        public string Color
        {
            get { return color; }
            set { color = value; ChangeDescription(); }
        } 
        #endregion

        public void ChangeDescription()
        {
            if (DesignMode)
                return;

            SuspendLayout();
            UpdateTag();
            RemoveTags();
            ResumeLayout();
        }

        private void UpdateTag()
        {
            Text = OriginalValue;
            Replace(tagCategory, category);
            Replace(tagDesigner, designer);
            Replace(tagSize, size);
            Replace(tagColor, color);
            Replace(tagPrint, print);
            Replace(tagMaterial, material);
        }

        private void Replace(string oldvalue, string newvalue, bool force = false)
        {
            string prefix;

            if (!force && (Text.Length == 0 || oldvalue.IsEmpty() || newvalue.IsEmpty()))
                return;

            Regex regEx = new Regex(string.Format("\\{{{0}.*?\\}}", oldvalue), RegexOptions.IgnoreCase);
            MatchCollection mcoll = regEx.Matches(Text);
            foreach (Match mc in mcoll)
            {
                if (mc.Success)
                {
                    int pos = mc.Value.IndexOf(":");
                    if (pos>0)
                    {
                        prefix = Regex.Replace(mc.Value.Substring(pos + 1), "[:}]", "") + " ";
                        if (!force)
                            newvalue = prefix + newvalue;
                    }
                    Text = Text.Replace(mc.ToString(), newvalue);
                }
            }
        }

        internal void RemoveTags()
        {
            Replace(tagCategory, "", true);
            Replace(tagDesigner, "", true);
            Replace(tagSize, "", true);
            Replace(tagColor, "", true);
            Replace(tagPrint, "", true);
            Replace(tagMaterial, "", true);

            Text = Regex.Replace(Text, " {2,4}", " ");
            Text = Text.TrimAll();
        }
    }
}
